//
//  GradiendNode.swift
//  dcloudlive
//
//  Created by zhu xietong on 2020/5/6.
//  Copyright © 2020 douyun. All rights reserved.
//

import Foundation
import AsyncDisplayKit
open class ShadowNode:MoBaseNode{
    open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        if #available(iOS 13.0, *) {
            switch UITraitCollection.current.userInterfaceStyle {
            case .dark:
                layer.shadowColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 0.15).cgColor
            default:
                layer.shadowColor = UIColor(red: 0.5, green: 0.55, blue: 0.57, alpha: 0.1).cgColor
            }
        } else {
            // Fallback on earlier versions
        }
    }
    
  
    
    open override func initStyle() {
        backgroundColor = .clear
        clipsToBounds = false
        layer.shadowColor = UIColor(red: 0, green: 0.05, blue: 0.27, alpha: 0.2).cgColor
        layer.shadowOffset = CGSize(width: 0, height: 7.78)
        layer.shadowOpacity = 1
        layer.shadowRadius = 6
        // fill
        backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
        layer.cornerRadius = 6;
        alpha = 1
    }
    
}





open class GradientNode:MoBaseNode{
    public var colors:[CGColor] = [UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor, UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor]
    {
        didSet{
            (gradient.view as? Gradiend)?.colors = colors
        }
    }
    public var locations:[NSNumber] = [0,1]
       {
           didSet{
            (gradient.view as? Gradiend)?.bgGradient.locations = locations
           }
       }
    
    open class Gradiend: UIView {
        public let bgGradient = CAGradientLayer()
        
        public override init(frame: CGRect) {
            super.init(frame: frame)
            initStyle()
        }
        public init() {
            super.init(frame: .zero)
            initStyle()
        }
        
        required public init?(coder: NSCoder) {
            super.init(coder: coder)
            initStyle()
        }
        var colors:[CGColor] = [UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor,UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor, UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor]
        {
            didSet{
                var ls = [NSNumber]()
                var i:Int = -1
                        
                
                for _ in colors {
                    i = i + 1
                    ls.append(NSNumber(integerLiteral: i))
                }
                
                bgGradient.locations = ls

                bgGradient.colors = colors
            }
        }
        
        open func initStyle() {
//            let bgGradient = CAGradientLayer()
            bgGradient.colors = colors
            
            
            bgGradient.locations = [0, 1]
            bgGradient.startPoint = CGPoint(x: 0.5, y: 0)
            bgGradient.endPoint = CGPoint(x: 0.5, y: 1)
            isUserInteractionEnabled = false
            
            layer.addSublayer(bgGradient)

           
        }

        public override func draw(_ rect: CGRect) {
            super.draw(rect)
            bgGradient.frame = CGRect(x: 0, y: 0, width: rect.width, height: rect.height)
//            layer.cornerRadius = 20;
            alpha = 1
        }
    }
    public let gradient = ASDisplayNode { () -> UIView in
        return Gradiend()
    }
    public var gradientLayer:CAGradientLayer?{
        return (gradient.view as? Gradiend)?.bgGradient
    }
    
    open override func specGetter() -> _Spec {
        return gradient.Spec
    }
}
